package reponse

import (
	"net/http"

	"gitee.com/dayphosphor/go-utils/errorx"
	"github.com/zeromicro/go-zero/rest/httpx"
)
type RespData struct {
	Code int32         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}
func Response(w http.ResponseWriter, resp interface{}, err error) {
	var body RespData
	if err != nil {
		if e, ok := err.(*errorx.CodeError); ok { //自定义错误类型
			//自定义CodeError
			body.Code = e.GetErrCode()
			body.Msg = e.GetMsg()
		} else {
			codeErr := errorx.NewErrMsg(err.Error())
			body.Code = codeErr.GetErrCode()
			body.Msg = codeErr.GetMsg()
		}

		body.Data = resp
	} else {
		body.Msg = "OK"
		body.Data = resp
	}

    //w.Header().Set("Access-Control-Allow-Origin", "*")
	//w.Header().Set("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, authorization, beid, ptyid")
	//w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
	//w.Header().Set("Access-Control-Expose-Headers", "Content-Length, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
	//w.Header().Set("Access-Control-Allow-Credentials", "true")

	httpx.OkJson(w, body)
}